package com.ruoyi.quartz.task;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.domain.WdyVcFundAssetDailyProfit;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.IWdyKlineService;
import com.ruoyi.system.service.IWdyUserService;
import com.ruoyi.system.service.IWdyVcFundAssetDailyProfitService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component("vcFundAssetTask")
public class VcFundAssetTask {
    protected static Logger log = LoggerFactory.getLogger(VcFundAssetTask.class);
    @Autowired
    private IWdyUserService wdyUserService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IWdyKlineService wdyKlineService;
    @Autowired
    private IWdyVcFundAssetDailyProfitService wdyVcFundAssetDailyProfitService;
    public void calDailyProfit() {
        Date yesDate = DateUtils.addDays(new Date(), -1);
        String  dateStr = DateUtils.format(yesDate, DateUtils.YYYY_MM_DD);

        QueryWrapper<WdyUser> userQuery = new QueryWrapper<>();
        userQuery.select("user_id", "vc_fund_asset");
        userQuery.gt("vc_fund_asset", 0);
        List<WdyUser> userList = wdyUserService.list(userQuery);
        if(CollectionUtils.isEmpty(userList)) {
           return;
        }
        BigDecimal dailyProfitRatio = new BigDecimal(sysConfigService.selectConfigByKey("vc_fund_asset_daily_profit_ratio"));
        BigDecimal vcPrice = wdyKlineService.getYestodayPrice();

        for(WdyUser user : userList) {
            QueryWrapper<WdyVcFundAssetDailyProfit> vcDailyProfitQuery = new QueryWrapper<>();
            vcDailyProfitQuery.eq("user_id", user.getUserId());
            vcDailyProfitQuery.eq("grant_date", dateStr);
            if(wdyVcFundAssetDailyProfitService.count(vcDailyProfitQuery) > 0) {
                continue;
            }
            BigDecimal dailyProfit = BigDecimalUtil.getP3Decimal(user.getVcFundAsset().multiply(dailyProfitRatio).multiply(vcPrice));
            if(dailyProfit.compareTo(BigDecimal.ZERO) <= 0) {
                continue;
            }
            WdyVcFundAssetDailyProfit vcFundAssetDailyProfit = new WdyVcFundAssetDailyProfit();
            vcFundAssetDailyProfit.setUserId(user.getUserId());
            vcFundAssetDailyProfit.setVcFundAsset(user.getVcFundAsset());
            vcFundAssetDailyProfit.setProfit(dailyProfit);
            vcFundAssetDailyProfit.setProfitRatio(dailyProfitRatio);
            vcFundAssetDailyProfit.setVcPrice(vcPrice);
            vcFundAssetDailyProfit.setGrantDate(yesDate);
            for(int i = 0; i < 100; i++) {
                try {
                    wdyVcFundAssetDailyProfitService.grantDailyProfit(vcFundAssetDailyProfit);
                    break;
                } catch (Exception e) {
                    log.error("发放基金日收益异常:" + JSON.toJSON(vcFundAssetDailyProfit), e);
                }
            }
        }
    }
}
